package st.strategy.vo;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.math3.stat.descriptive.SynchronizedDescriptiveStatistics;
import org.apache.commons.math3.util.FastMath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import st.BasicStockData;
import st.base.Sysdate;

public class Dmi implements TechLine {
	private Map<Sysdate, Double> trData;
	private Map<Sysdate, Double> plusDmData;
	private Map<Sysdate, Double> netativeDmData;
	private Collection<BasicStockData> stockDatas;
	private SynchronizedDescriptiveStatistics plusAdm; 
	
	private int n;
	private Logger logger = LoggerFactory.getLogger(getClass());

	public Dmi(Collection<BasicStockData> stockData, int n) {
		trData = new HashMap<Sysdate, Double>();
		plusDmData = new HashMap<Sysdate, Double>();
		netativeDmData = new HashMap<Sysdate, Double>();
		this.stockDatas = stockData;
		this.n = n;
		try {
			buildTrData();
			buildDm();
		} catch (Exception e) {
			logger.error(ExceptionUtils.getStackTrace(e));
		}

	}

	private void buildDm() {
		BasicStockData lastStockData = null;
		for (BasicStockData stockData : stockDatas) {
			if (lastStockData == null) {
				lastStockData = stockData;
			}
			Sysdate tradeDate = stockData.getTradeDate();
			double plusDm = stockData.getHighest() - lastStockData.getHighest();
			plusDm = (plusDm < 0 ? 0 : plusDm);
			plusDmData.put(tradeDate, plusDm);
			double negativeDm = stockData.getLowest()
					- lastStockData.getLowest();
			negativeDm = (negativeDm > 0 ? 0 : negativeDm);
			netativeDmData.put(tradeDate, negativeDm);
		}

	}

	private void buildTrData() {
		BasicStockData lastStockDaa = null;
		for (BasicStockData stockData : this.stockDatas) {
			double todayDiff = FastMath.abs(stockData.getHighest()
					- stockData.getLowest());
			double lastMax = (lastStockDaa == null ? 0 : lastStockDaa
					.getHighest());
			double lastMin = (lastStockDaa == null ? 0 : lastStockDaa
					.getLowest());
			double upDiff = FastMath.abs(stockData.getClose() - lastMax);
			double downDiff = FastMath.abs(stockData.getClose() - lastMin);
			double tr = NumberUtils.max(todayDiff, upDiff, downDiff);
			trData.put(stockData.getTradeDate(), tr);

		}
	}

	@Override
	public TechValue getValue(Sysdate date) {
		// TODO Auto-generated method stub
		return null;
	}

}
